package cz.khfotbal.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.khfotbal.domain.Competion;
import cz.khfotbal.domain.Match;
import cz.khfotbal.domain.Referee;
import cz.khfotbal.domain.Season;
import cz.khfotbal.domain.Team;
import cz.khfotbal.service.CompetionService;
import cz.khfotbal.service.DelegatService;
import cz.khfotbal.service.MatchService;
import cz.khfotbal.service.PlayerService;
import cz.khfotbal.service.RefereeService;
import cz.khfotbal.service.SeasonService;
import cz.khfotbal.service.TeamService;

@Component
@Scope(value = "session")
public class MatchBean {

	@Autowired
	private TeamService teamService;
	@Autowired
	private PlayerService playerService;
	@Autowired
	private SeasonService seasonService;
	@Autowired
	private CompetionService competionService;
	@Autowired
	private MatchService matchService;
	@Autowired
	private DelegatService delegatService;
	@Autowired
	private RefereeService refereeService;

	private List<String> seasonNameList = new ArrayList<String>();
	private List<Season> seasonList = new ArrayList<Season>();
	private String selectedSeasonName = "";
	private Season selectedSeason;
	private List<String> competionNameList = new ArrayList<String>();
	private List<Competion> competionList = new ArrayList<Competion>();
	private String selectedCompetionName = "";
	private Competion selectedCompetion;
	private List<Match> matchList = new ArrayList<Match>();
	private Match selectedMatch;
	private List<Team> teamList = new ArrayList<Team>();
	private List<String> teamNameList = new ArrayList<String>();
	private String selectedHomeTeamName = "";
	private String selectedAwayTeamName = "";
	private List<Referee> refereeList = new ArrayList<Referee>();
	private String matchStart;
	private String matchDatum;
	private Integer round;
	private String matchCode;
	
	private List<String> bergerRoundList;
	
	private int bergerPom;
	private String delegationDate;
	
	private String dateOne;
	private String dateTwo;
	private String dateThree;
	private String dateFour;
	private String dateFive;
	
	private String timeOne;
	private String timeTwo;
	private String timeThree;
	private String timeFour;
	private String timeFive;
	
	private int roundOne;
	private int roundTwo;
	private int roundThree;
	private int roundFour;
	private int roundFive;
	
	private int numOfTeams;
	
	private String code;

	public void deleteMatch() {
		matchService.delete(selectedMatch);
		fillMatchList();
		selectedMatch = new Match();
	}

	public void fillMatchList() {
		selectedSeason = getSeasonByName(selectedSeasonName);
		selectedCompetion = getCompetionByName(selectedCompetionName);
		matchList = matchService.getMatchesBySeasonAndCompetion(selectedSeason,
				selectedCompetion);
		teamList = teamService.getTeamsBySeasonAndCompetion(selectedSeason,
				selectedCompetion);
		teamNameList = new ArrayList<String>();
		for (Team t : teamList) {
			teamNameList.add(t.getName());
		}
		selectedMatch = new Match();
		bergerRoundList = new ArrayList<String>();
		
		for (int i=1; i<31; i++){
			bergerRoundList.add(Integer.toString(i));
		}
	}
	
	public void fillMatchListDate() {
//		selectedSeason = getSeasonByName(selectedSeasonName);
//		selectedCompetion = getCompetionByName(selectedCompetionName);
		matchList = matchService.getMatchesByDate(delegationDate);
	}
	
	public void fillMatchListThisWeek() {
//		selectedSeason = getSeasonByName(selectedSeasonName);
//		selectedCompetion = getCompetionByName(selectedCompetionName);
		Date actualDate = new Date();
		int dataShift = 6-actualDate.getDay();
		actualDate.setDate(actualDate.getDate()+dataShift);
		System.out.println(stringDate(actualDate));
		matchList = matchService.getMatchesByDate(stringDate(actualDate));
		actualDate.setDate(actualDate.getDate()+1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
	}
	
	public void fillMatchListNextWeek() {
//		selectedSeason = getSeasonByName(selectedSeasonName);
//		selectedCompetion = getCompetionByName(selectedCompetionName);
		Date actualDate = new Date();
		int dataShift = 13-actualDate.getDay();
		actualDate.setDate(actualDate.getDate()+dataShift);
		System.out.println(stringDate(actualDate));
		matchList = matchService.getMatchesByDate(stringDate(actualDate));
		actualDate.setDate(actualDate.getDate()+1);
		matchList.addAll(matchService.getMatchesByDate(stringDate(actualDate)));
	}

	public void updateMatch() {
		matchService.updateMatch(selectedMatch);
		fillMatchList();
		selectedMatch = new Match();
	}

	public void addMatchToCompetion() {
		Team homeTeam = getTeamByName(selectedHomeTeamName);
		Team awayTeam = getTeamByName(selectedAwayTeamName);
		Match match = new Match();
		match.setRound(round);
		match.setStart(matchStart);
		match.setDatum(matchDatum);
		match.setMatchCode(matchCode);
		match.setDelegat("");
		match.setSpolujizda("");
		match.setMainReferee("");
		match.setLineReferee("");
		match.setLineRefereeTwo("");
		matchService.createMatch(homeTeam, awayTeam, selectedCompetion,
				selectedSeason, match);
		fillMatchList();
		selectedMatch = new Match();
	}

	public List<String> getSeasonNamesList() {
		seasonNameList = new ArrayList<String>();
		seasonList = seasonService.getAllSeasons();
		for (Season season : seasonList) {
			seasonNameList.add(season.getSeasonName());
		}
		return seasonNameList;
	}

	public void handleSeasonChange() {
		if (selectedSeasonName.equals("")) {
			competionNameList = new ArrayList<String>();
			return;
		}
		competionList = competionService
				.getCompetionsBySeason(getSeasonByName(selectedSeasonName));
		competionNameList = new ArrayList<String>();
		for (Competion c : competionList) {
			competionNameList.add(c.getName());
		}
	}

	public Season getSeasonByName(String name) {
		for (Season s : seasonList) {
			if (name.equals(s.getSeasonName())) {
				return s;
			}
		}
		return null;
	}

	public Referee getRefereeByName(String name) {
		String[] slist = name.split(" ");
		for (Referee r : refereeList) {
			if (r.getFirstname().equals(slist[0])
					&& r.getSurname().equals(slist[1])) {
				return r;
			}
		}
		return null;
	}

	public Competion getCompetionByName(String name) {
		for (Competion c : competionList) {
			if (name.equals(c.getName())) {
				return c;
			}
		}
		return null;
	}

	public Team getTeamByName(String name) {
		for (Team t : teamList) {
			if (name.equals(t.getName())) {
				return t;
			}
		}
		return null;
	}
	
	public void fillBergerEight(){
		numOfTeams = 8;
	}
	public void fillBergerFourteen(){
		numOfTeams = 14;
	}
	public void fillBergerSixteen(){
		numOfTeams = 16;
	}
	
	public void bergerTabSixteen(){
		boolean numberTest = false;
		List<Team> teams = teamService.getTeamsBySeasonAndCompetion(selectedSeason,
				selectedCompetion);
		
		if (numOfTeams==16){
			int control = 0;
			for (Team team : teams){
				control = control + team.getNumber();
				if (control==136 && teams.size()==16){
					numberTest = true;
				}
			}
		}
		
		if (numOfTeams==14){
			int control = 0;
			for (Team team : teams){
				control = control + team.getNumber();
				if (control==105 && teams.size()==14){
					numberTest = true;
				}
			}
		}
		
		if (numOfTeams==8){
			int control = 0;
			for (Team team : teams){
				control = control + team.getNumber();
				if (control==36 && teams.size()==8){
					numberTest = true;
				}
			}
		}
		
		if (numberTest){
			HashMap<String, Team> teamMap = new HashMap<String, Team>();
			for (Team team : teams){
				teamMap.put(Integer.toString(team.getNumber()), team);
			}		
			
			String date = "";
			String time = "";
			for (int j=1; j<6; j++){
				if ((j==1 & !dateOne.equals("")) || (j==2 & !dateTwo.equals("")) || (j==3 & !dateThree.equals("")) || (j==4 & !dateFour.equals("")) || (j==5 & !dateFive.equals(""))){
					if (j==1){
						date = dateOne;
						time = timeOne;
						bergerPom = roundOne;
					}
					if (j==2){
						date = dateTwo;
						time = timeTwo;
						bergerPom = roundTwo;
					}
					if (j==3){
						date = dateThree;
						time = timeThree;
						bergerPom = roundThree;
					}
					if (j==4){
						date = dateFour;
						time = timeFour;
						bergerPom = roundFour;
					}
					if (j==5){
						date = dateFive;
						time = timeFive;
						bergerPom = roundFive;
					}
				
					if (numOfTeams == 16){
						int i = 0;
						int bergerChoose = bergerPom;
						if (bergerPom>15){
							i = 1;
							bergerChoose=bergerChoose-15;
						}
				
						switch(bergerChoose)
						{
						case 1: 	
						addBergerMatchToCompetion(1, 16, 1, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 15, 1, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 14, 1, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 13, 1, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 12, 1, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 11, 1, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 10, 1, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 9, 1, 8, teamMap, i, date, time);
						break;
						case 2:
						addBergerMatchToCompetion(16, 9, 2, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 8, 2, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 7, 2, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 6, 2, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 5, 2, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 4, 2, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 3, 2, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 2, 2, 8, teamMap, i, date, time);
						break;
						case 3:
						addBergerMatchToCompetion(2, 16, 3, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 1, 3, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 15, 3, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 14, 3, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 13, 3, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 12, 3, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 11, 3, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 10, 3, 8, teamMap, i, date, time);
						break;
						case 4:
						addBergerMatchToCompetion(16, 10, 4, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 9, 4, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 8, 4, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 7, 4, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 6, 4, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 5, 4, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 4, 4, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 3, 4, 8, teamMap, i, date, time);
						break;
						case 5:
						addBergerMatchToCompetion(3, 16, 5, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 2, 5, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 1, 5, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 15, 5, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 14, 5, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 13, 5, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 12, 5, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 11, 5, 8, teamMap, i, date, time);
						break;
						case 6:
						addBergerMatchToCompetion(16, 11, 6, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 10, 6, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 9, 6, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 8, 6, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 7, 6, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 6, 6, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 5, 6, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 4, 6, 8, teamMap, i, date, time);
						break;
						case 7:
						addBergerMatchToCompetion(4, 16, 7, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 3, 7, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 2, 7, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 1, 7, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 15, 7, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 14, 7, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 13, 7, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 12, 7, 8, teamMap, i, date, time);
						break;
						case 8:
						addBergerMatchToCompetion(16, 12, 8, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 11, 8, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 10, 8, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 9, 8, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 8, 8, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 7, 8, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 6, 8, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 5, 8, 8, teamMap, i, date, time);
						break;
						case 9:
						addBergerMatchToCompetion(5, 16, 9, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 4, 9, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 3, 9, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 2, 9, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 1, 9, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 15, 9, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 14, 9, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 13, 9, 8, teamMap, i, date, time);
						break;
						case 10:
						addBergerMatchToCompetion(16, 13, 10, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 12, 10, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 11, 10, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 10, 10, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 9, 10, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 8, 10, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 7, 10, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 6, 10, 8, teamMap, i, date, time);
						break;
						case 11:
						addBergerMatchToCompetion(6, 16, 11, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 5, 11, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 4, 11, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 3, 11, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 2, 11, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 1, 11, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 15, 11, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 14, 11, 8, teamMap, i, date, time);
						break;
						case 12:
						addBergerMatchToCompetion(16, 14, 12, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 13, 12, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 12, 12, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 11, 12, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 10, 12, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 9, 12, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 8, 12, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 7, 12, 8, teamMap, i, date, time);
						break;
						case 13:
						addBergerMatchToCompetion(7, 16, 13, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 6, 13, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 5, 13, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 4, 13, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 3, 13, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 2, 13, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 1, 13, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 15, 13, 8, teamMap, i, date, time);
						break;
						case 14:
						addBergerMatchToCompetion(16, 15, 14, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 14, 14, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 13, 14, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 12, 14, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 11, 14, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 10, 14, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 9, 14, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 8, 14, 8, teamMap, i, date, time);
						break;
						case 15:
						addBergerMatchToCompetion(8, 16, 15, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 7, 15, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 6, 15, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 5, 15, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 4, 15, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 3, 15, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(14, 2, 15, 7, teamMap, i, date, time);
						addBergerMatchToCompetion(15, 1, 15, 8, teamMap, i, date, time);
						}
					}
					
					if (numOfTeams == 14){
						int i = 0;
						int bergerChoose = bergerPom;
						if (bergerPom>13){
							i = 1;
							bergerChoose=bergerChoose-13;
						}
				
						switch(bergerChoose)
						{
						case 1: 	
						addBergerMatchToCompetion(1, 14, 1, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 13, 1, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 12, 1, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 11, 1, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 10, 1, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 9, 1, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 8, 1, 7, teamMap, i, date, time);
						break;
						
						case 2: 	
						addBergerMatchToCompetion(14, 8, 2, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 7, 2, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 6, 2, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 5, 2, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 4, 2, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 3, 2, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 2, 2, 7, teamMap, i, date, time);
						break;
						
						case 3: 	
						addBergerMatchToCompetion(2, 14, 3, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 1, 3, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 13, 3, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 12, 3, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 11, 3, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 10, 3, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 9, 3, 7, teamMap, i, date, time);
						break;
						
						case 4: 	
						addBergerMatchToCompetion(14, 9, 4, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 8, 4, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 7, 4, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 6, 4, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 5, 4, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 4, 4, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 3, 4, 7, teamMap, i, date, time);
						break;
						
						case 5: 	
						addBergerMatchToCompetion(3, 14, 5, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 2, 5, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 1, 5, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 13, 5, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 12, 5, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 11, 5, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 10, 5, 7, teamMap, i, date, time);
						break;
						
						case 6: 	
						addBergerMatchToCompetion(14, 10, 6, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 9, 6, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 8, 6, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 7, 6, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 6, 6, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 5, 6, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 4, 6, 7, teamMap, i, date, time);
						break;
						
						case 7: 	
						addBergerMatchToCompetion(4, 14, 7, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 3, 7, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 2, 7, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 1, 7, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 13, 7, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 12, 7, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 11, 7, 7, teamMap, i, date, time);
						break;
						
						case 8: 	
						addBergerMatchToCompetion(14, 11, 8, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 10, 8, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 9, 8, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 8, 8, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 7, 8, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 6, 8, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 5, 8, 7, teamMap, i, date, time);
						break;
						
						case 9: 	
						addBergerMatchToCompetion(5, 14, 9, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 4, 9, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 3, 9, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 2, 9, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 1, 9, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 13, 9, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 12, 9, 7, teamMap, i, date, time);
						break;
						
						case 10: 	
						addBergerMatchToCompetion(14, 12, 10, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 11, 10, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 10, 10, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 9, 10, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 8, 10, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 7, 10, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 6, 10, 7, teamMap, i, date, time);
						break;
						
						case 11: 	
						addBergerMatchToCompetion(6, 14, 11, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 5, 11, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 4, 11, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 3, 11, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 2, 11, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 1, 11, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 13, 11, 7, teamMap, i, date, time);
						break;
						
						case 12: 	
						addBergerMatchToCompetion(14, 13, 12, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 12, 12, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 11, 12, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 10, 12, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 9, 12, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 8, 12, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 7, 12, 7, teamMap, i, date, time);
						break;
						
						case 13: 	
						addBergerMatchToCompetion(7, 14, 13, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(8, 6, 13, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(9, 5, 13, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(10, 4, 13, 4, teamMap, i, date, time);
						addBergerMatchToCompetion(11, 3, 13, 5, teamMap, i, date, time);
						addBergerMatchToCompetion(12, 2, 13, 6, teamMap, i, date, time);
						addBergerMatchToCompetion(13, 1, 13, 7, teamMap, i, date, time);
						break;
						}
					}	
					
					if (numOfTeams == 8){
						int i = 0;
						int bergerChoose = bergerPom;
						if (bergerPom>7){
							i = 1;
							bergerChoose=bergerChoose-7;
						}
				
						switch(bergerChoose)
						{
						case 1: 	
						addBergerMatchToCompetion(1, 8, 1, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 7, 1, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 6, 1, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 5, 1, 4, teamMap, i, date, time);
						break;
						
						case 2: 	
						addBergerMatchToCompetion(8, 5, 2, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 4, 2, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 3, 2, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 2, 2, 4, teamMap, i, date, time);
						break;
						
						case 3: 	
						addBergerMatchToCompetion(2, 8, 3, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 1, 3, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 7, 3, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 6, 3, 4, teamMap, i, date, time);
						break;
						
						case 4: 	
						addBergerMatchToCompetion(8, 6, 4, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 5, 4, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 4, 4, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 3, 4, 4, teamMap, i, date, time);
						break;
						
						case 5: 	
						addBergerMatchToCompetion(3, 8, 5, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(4, 2, 5, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 1, 5, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 7, 5, 4, teamMap, i, date, time);
						break;
						
						case 6: 	
						addBergerMatchToCompetion(8, 7, 6, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(1, 6, 6, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(2, 5, 6, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(3, 4, 6, 4, teamMap, i, date, time);
						break;
						
						case 7: 	
						addBergerMatchToCompetion(4, 8, 7, 1, teamMap, i, date, time);
						addBergerMatchToCompetion(5, 3, 7, 2, teamMap, i, date, time);
						addBergerMatchToCompetion(6, 2, 7, 3, teamMap, i, date, time);
						addBergerMatchToCompetion(7, 1, 7, 4, teamMap, i, date, time);
						break;
						}
					}	
				}
			}
		}
		dateOne = "";
		dateTwo = "";
		dateThree = "";
		dateFour = "";
		dateFive = "";
		
		roundOne = 0;
		roundTwo = 0;
		roundThree = 0;
		roundFour = 0;
		roundFive = 0;
		
		timeOne = "";
		timeTwo = "";
		timeThree = "";
		timeFour = "";
		timeFive = "";
	}
	
	public void addBergerMatchToCompetion(int nHomeTeam, int nAwayTeam, int round, int matchNum, HashMap teamMap, int choose, String date, String time) {
//		tady urcit kolikateho presne je sobota prvniho kola
		Date firstRoundDate = new Date(2012-1900, 8-1, 11);
		
		int dataShift;
		
		Date dateOfRound = dateString(date);
		int dayOfRound = dateOfRound.getDay();
		
		Team homeTeam;
		Team awayTeam;
		if (choose==0){
			homeTeam = (Team) teamMap.get(Integer.toString(nHomeTeam));
			awayTeam = (Team) teamMap.get(Integer.toString(nAwayTeam));
		} else {
			homeTeam = (Team) teamMap.get(Integer.toString(nAwayTeam));
			awayTeam = (Team) teamMap.get(Integer.toString(nHomeTeam));
			round = round+numOfTeams-1;
		}
		
		String matchCode = getMatchCode(round, matchNum, code);
		
		String sPlayDay = "neni den";
		if (dayOfRound!=6){
			sPlayDay = date;
		}
		if ((homeTeam.getPlayDay().equals("sobota") && dayOfRound==6) || (homeTeam.getPlayDay().equals("nedele") && dayOfRound==6)){
			Date playDay = (Date) dateOfRound.clone();
			if (homeTeam.getPlayDay().equals("nedele")){
				playDay.setDate(playDay.getDate()+1);
			}
			
			String day;
			String month;
			int iDay = playDay.getDate();
			if (iDay<10){
				day = "0"+iDay;
			} else {
				day = Integer.toString(iDay);
			}
			
			int iMonth = playDay.getMonth()+1;
			if (iMonth<10){
				month = "0"+iMonth;
			} else {
				month = Integer.toString(iMonth);
			}
			String year = Integer.toString(playDay.getYear()+1900);
			sPlayDay = day+"."+month+"."+year;
		}
		
		String playTime = time;
		if (!homeTeam.getPlayTime().equals(null) && !homeTeam.getPlayTime().equals("")){
			playTime = homeTeam.getPlayTime();
		} 
		
		
		
		Match match = new Match();
		match.setRound(round);
		match.setStart(playTime);
		match.setDatum(sPlayDay);
		match.setMatchCode(matchCode);
		match.setDelegat("");
		match.setSpolujizda("");
		match.setMainReferee("");
		match.setLineReferee("");
		match.setLineRefereeTwo("");
		matchService.createMatch(homeTeam, awayTeam, selectedCompetion,
				selectedSeason, match);
		fillMatchList();
		selectedMatch = new Match();
	}
	
	private String getMatchCode(int round, int matchNum, String code){
		String sRound = "";
		if (round<10){
			sRound = "0"+round;
		} else {
			sRound = Integer.toString(round);
		}
		
		String sMatchNum = "0"+matchNum;
		
		String matchCode = code+sRound+sMatchNum;
		return matchCode;
	}
	
	private String stringDate(Date date){
		String day;
		String month;
		int iDay = date.getDate();
		if (iDay<10){
			day = "0"+iDay;
		} else {
			day = Integer.toString(iDay);
		}
		
		int iMonth = date.getMonth()+1;
		if (iMonth<10){
			month = "0"+iMonth;
		} else {
			month = Integer.toString(iMonth);
		}
		String year = Integer.toString(date.getYear()+1900);
		String sPlayDay = day+"."+month+"."+year;
		return sPlayDay;
	}
	
	private Date dateString(String sDate){
		System.out.println(sDate);
		String[] split = sDate.split("\\.");
		int year = Integer.parseInt(split[2]);
		int month = Integer.parseInt(split[1]);
		int day = Integer.parseInt(split[0]);
		Date date = new Date(year-1900, month-1, day);
		return date;
	}

	public CompetionService getCompetionService() {
		return competionService;
	}

	public void setCompetionService(CompetionService competionService) {
		this.competionService = competionService;
	}

	public List<String> getSeasonNameList() {
		return seasonNameList;
	}

	public void setSeasonNameList(List<String> seasonNameList) {
		this.seasonNameList = seasonNameList;
	}

	public List<Season> getSeasonList() {
		return seasonList;
	}

	public void setSeasonList(List<Season> seasonList) {
		this.seasonList = seasonList;
	}

	public String getSelectedSeasonName() {
		return selectedSeasonName;
	}

	public void setSelectedSeasonName(String selectedSeasonName) {
		this.selectedSeasonName = selectedSeasonName;
	}

	public Season getSelectedSeason() {
		return selectedSeason;
	}

	public void setSelectedSeason(Season selectedSeason) {
		this.selectedSeason = selectedSeason;
	}

	public List<String> getCompetionNameList() {
		return competionNameList;
	}

	public void setCompetionNameList(List<String> competionNameList) {
		this.competionNameList = competionNameList;
	}

	public List<Competion> getCompetionList() {
		return competionList;
	}

	public void setCompetionList(List<Competion> competionList) {
		this.competionList = competionList;
	}

	public String getSelectedCompetionName() {
		return selectedCompetionName;
	}

	public void setSelectedCompetionName(String selectedCompetionName) {
		this.selectedCompetionName = selectedCompetionName;
	}

	public Competion getSelectedCompetion() {
		return selectedCompetion;
	}

	public void setSelectedCompetion(Competion selectedCompetion) {
		this.selectedCompetion = selectedCompetion;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public SeasonService getSeasonService() {
		return seasonService;
	}

	public void setSeasonService(SeasonService seasonService) {
		this.seasonService = seasonService;
	}

	public MatchService getMatchService() {
		return matchService;
	}

	public void setMatchService(MatchService matchService) {
		this.matchService = matchService;
	}

	public List<Match> getMatchList() {
		return matchList;
	}

	public void setMatchList(List<Match> matchList) {
		this.matchList = matchList;
	}

	public Match getSelectedMatch() {
		if (selectedMatch==null){
			selectedMatch = new Match();
		}
		return selectedMatch;
	}

	public void setSelectedMatch(Match selectedMatch) {
		this.selectedMatch = selectedMatch;
	}

	public List<Team> getTeamList() {
		return teamList;
	}

	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	public List<String> getTeamNameList() {
		return teamNameList;
	}

	public void setTeamNameList(List<String> teamNameList) {
		this.teamNameList = teamNameList;
	}

	public String getSelectedHomeTeamName() {
		return selectedHomeTeamName;
	}

	public void setSelectedHomeTeamName(String selectedHomeTeamName) {
		this.selectedHomeTeamName = selectedHomeTeamName;
	}

	public String getSelectedAwayTeamName() {
		return selectedAwayTeamName;
	}

	public void setSelectedAwayTeamName(String selectedAwayTeamName) {
		this.selectedAwayTeamName = selectedAwayTeamName;
	}

	public DelegatService getDelegatService() {
		return delegatService;
	}

	public void setDelegatService(DelegatService delegatService) {
		this.delegatService = delegatService;
	}

	public RefereeService getRefereeService() {
		return refereeService;
	}

	public void setRefereeService(RefereeService refereeService) {
		this.refereeService = refereeService;
	}

	public List<Referee> getRefereeList() {
		return refereeList;
	}

	public void setRefereeList(List<Referee> refereeList) {
		this.refereeList = refereeList;
	}

	public String getMatchStart() {
		return matchStart;
	}

	public void setMatchStart(String matchStart) {
		this.matchStart = matchStart;
	}

	public Integer getRound() {
		return round;
	}

	public void setRound(Integer round) {
		this.round = round;
	}

	public PlayerService getPlayerService() {
		return playerService;
	}

	public void setPlayerService(PlayerService playerService) {
		this.playerService = playerService;
	}

	public String getMatchCode() {
		return matchCode;
	}

	public void setMatchCode(String matchCode) {
		this.matchCode = matchCode;
	}

	public String getMatchDatum() {
		return matchDatum;
	}

	public void setMatchDatum(String matchDatum) {
		this.matchDatum = matchDatum;
	}

	public int getBergerPom() {
		return bergerPom;
	}

	public void setBergerPom(int bergerPom) {
		this.bergerPom = bergerPom;
	}

	public List<String> getBergerRoundList() {
		return bergerRoundList;
	}

	public void setBergerRoundList(List<String> bergerRoundList) {
		this.bergerRoundList = bergerRoundList;
	}

	public String getDelegationDate() {
		return delegationDate;
	}

	public void setDelegationDate(String delegationDate) {
		this.delegationDate = delegationDate;
	}

	public String getDateOne() {
		return dateOne;
	}

	public void setDateOne(String dateOne) {
		this.dateOne = dateOne;
	}

	public String getDateTwo() {
		return dateTwo;
	}

	public void setDateTwo(String dateTwo) {
		this.dateTwo = dateTwo;
	}

	public String getDateThree() {
		return dateThree;
	}

	public void setDateThree(String dateThree) {
		this.dateThree = dateThree;
	}

	public String getDateFour() {
		return dateFour;
	}

	public void setDateFour(String dateFour) {
		this.dateFour = dateFour;
	}

	public String getDateFive() {
		return dateFive;
	}

	public void setDateFive(String dateFive) {
		this.dateFive = dateFive;
	}

	public int getRoundOne() {
		return roundOne;
	}

	public void setRoundOne(int roundOne) {
		this.roundOne = roundOne;
	}

	public int getRoundTwo() {
		return roundTwo;
	}

	public void setRoundTwo(int roundTwo) {
		this.roundTwo = roundTwo;
	}

	public int getRoundThree() {
		return roundThree;
	}

	public void setRoundThree(int roundThree) {
		this.roundThree = roundThree;
	}

	public int getRoundFour() {
		return roundFour;
	}

	public void setRoundFour(int roundFour) {
		this.roundFour = roundFour;
	}

	public int getRoundFive() {
		return roundFive;
	}

	public void setRoundFive(int roundFive) {
		this.roundFive = roundFive;
	}

	public String getTimeOne() {
		return timeOne;
	}

	public void setTimeOne(String timeOne) {
		this.timeOne = timeOne;
	}

	public String getTimeTwo() {
		return timeTwo;
	}

	public void setTimeTwo(String timeTwo) {
		this.timeTwo = timeTwo;
	}

	public String getTimeThree() {
		return timeThree;
	}

	public void setTimeThree(String timeThree) {
		this.timeThree = timeThree;
	}

	public String getTimeFour() {
		return timeFour;
	}

	public void setTimeFour(String timeFour) {
		this.timeFour = timeFour;
	}

	public String getTimeFive() {
		return timeFive;
	}

	public void setTimeFive(String timeFive) {
		this.timeFive = timeFive;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
	

}